
import { createRouter, createWebHashHistory } from 'vue-router';

import { useAuth } from 'src/composables/auth';


function passRouteToProps(route) {
	return {
		queryParams: route.query,
		fieldName: route.params.fieldName,
		fieldValue: route.params.fieldValue
	}
}


let routes = [
	//Dashboard routes


	//audits routes
	{
		path: '/audits/:fieldName?/:fieldValue?',
		name: 'auditslist',
		component: () => import('./pages/audits/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/audits/view/:id',
		name: 'auditsview',
		component: () => import('./pages/audits/view.vue'),
		props: true
	},

	//auditslog routes  
	{
		path: '/auditslog',
		name: 'auditslog',
		component: () => import('./pages/auditslog/index.vue')
	},
	//exception routes                
	{
		path: '/exception',
		name: 'exception',
		component: () => import('./pages/exception/index.vue'),
		meta: {
			title: '系统监控 - 异常事件处理',
			requiresAuth: true
		}
	},


	//blocks routes
	{
		path: '/blocks/:fieldName?/:fieldValue?',
		name: 'blockslist',
		component: () => import('./pages/blocks/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/blocks/view/:id',
		name: 'blocksview',
		component: () => import('./pages/blocks/view.vue'),
		props: true
	},

	{
		path: '/blocks/add',
		name: 'blocksadd',
		component: () => import('./pages/blocks/add.vue'),
		props: true
	},

	{
		path: '/blocks/edit/:id',
		name: 'blocksedit',
		component: () => import('./pages/blocks/edit.vue'),
		props: true
	},


	//courses routes
	{
		path: '/courses/:fieldName?/:fieldValue?',
		name: 'courseslist',
		component: () => import('./pages/courses/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/courses/view/:id',
		name: 'coursesview',
		component: () => import('./pages/courses/view.vue'),
		props: true
	},

	{
		path: '/courses/add',
		name: 'coursesadd',
		component: () => import('./pages/courses/add.vue'),
		props: true
	},

	{
		path: '/courses/edit/:id',
		name: 'coursesedit',
		component: () => import('./pages/courses/edit.vue'),
		props: true
	},


	//enrollments routes
	{
		path: '/enrollments/:fieldName?/:fieldValue?',
		name: 'enrollmentslist',
		component: () => import('./pages/enrollments/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/enrollments/view/:id',
		name: 'enrollmentsview',
		component: () => import('./pages/enrollments/view.vue'),
		props: true
	},

	{
		path: '/enrollments/add',
		name: 'enrollmentsadd',
		component: () => import('./pages/enrollments/add.vue'),
		props: true
	},

	{
		path: '/enrollments/edit/:id',
		name: 'enrollmentsedit',
		component: () => import('./pages/enrollments/edit.vue'),
		props: true
	},


	//grades routes
	{
		path: '/grades/:fieldName?/:fieldValue?',
		name: 'gradeslist',
		component: () => import('./pages/grades/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/grades/view/:id',
		name: 'gradesview',
		component: () => import('./pages/grades/view.vue'),
		props: true
	},

	{
		path: '/grades/add',
		name: 'gradesadd',
		component: () => import('./pages/grades/add.vue'),
		props: true
	},

	{
		path: '/grades/edit/:id',
		name: 'gradesedit',
		component: () => import('./pages/grades/edit.vue'),
		props: true
	},


	//permissions routes
	{
		path: '/permissions/:fieldName?/:fieldValue?',
		name: 'permissionslist',
		component: () => import('./pages/permissions/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/permissions/view/:id',
		name: 'permissionsview',
		component: () => import('./pages/permissions/view.vue'),
		props: true
	},

	{
		path: '/permissions/add',
		name: 'permissionsadd',
		component: () => import('./pages/permissions/add.vue'),
		props: true
	},

	{
		path: '/permissions/edit/:id',
		name: 'permissionsedit',
		component: () => import('./pages/permissions/edit.vue'),
		props: true
	},


	//roles routes
	{
		path: '/roles/:fieldName?/:fieldValue?',
		name: 'roleslist',
		component: () => import('./pages/roles/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/roles/view/:id',
		name: 'rolesview',
		component: () => import('./pages/roles/view.vue'),
		props: true
	},

	{
		path: '/roles/add',
		name: 'rolesadd',
		component: () => import('./pages/roles/add.vue'),
		props: true
	},

	{
		path: '/roles/edit/:id',
		name: 'rolesedit',
		component: () => import('./pages/roles/edit.vue'),
		props: true
	},


	//smartcontracts routes
	{
		path: '/smartcontracts/:fieldName?/:fieldValue?',
		name: 'smartcontractslist',
		component: () => import('./pages/smartcontracts/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/smartcontracts/view/:id',
		name: 'smartcontractsview',
		component: () => import('./pages/smartcontracts/view.vue'),
		props: true
	},

	{
		path: '/smartcontracts/add',
		name: 'smartcontractsadd',
		component: () => import('./pages/smartcontracts/add.vue'),
		props: true
	},

	{
		path: '/smartcontracts/edit/:id',
		name: 'smartcontractsedit',
		component: () => import('./pages/smartcontracts/edit.vue'),
		props: true
	},


	//smartcontracttransactions routes
	{
		path: '/smartcontracttransactions/:fieldName?/:fieldValue?',
		name: 'smartcontracttransactionslist',
		component: () => import('./pages/smartcontracttransactions/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/smartcontracttransactions/view/:id',
		name: 'smartcontracttransactionsview',
		component: () => import('./pages/smartcontracttransactions/view.vue'),
		props: true
	},

	{
		path: '/smartcontracttransactions/add',
		name: 'smartcontracttransactionsadd',
		component: () => import('./pages/smartcontracttransactions/add.vue'),
		props: true
	},

	{
		path: '/smartcontracttransactions/edit/:id',
		name: 'smartcontracttransactionsedit',
		component: () => import('./pages/smartcontracttransactions/edit.vue'),
		props: true
	},


	//studentgradesummary routes
	{
		path: '/studentgradesummary/:fieldName?/:fieldValue?',
		name: 'studentgradesummarylist',
		component: () => import('./pages/studentgradesummary/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/studentgradesummary/view/:id',
		name: 'studentgradesummaryview',
		component: () => import('./pages/studentgradesummary/view.vue'),
		props: true
	},

	{
		path: '/studentgradesummary/add',
		name: 'studentgradesummaryadd',
		component: () => import('./pages/studentgradesummary/add.vue'),
		props: true
	},

	{
		path: '/studentgradesummary/edit/:id',
		name: 'studentgradesummaryedit',
		component: () => import('./pages/studentgradesummary/edit.vue'),
		props: true
	},


	//users routes
	{
		path: '/users/:fieldName?/:fieldValue?',
		name: 'userslist',
		component: () => import('./pages/users/list.vue'),
		props: route => passRouteToProps(route)
	},

	{
		path: '/users/view/:id',
		name: 'usersview',
		component: () => import('./pages/users/view.vue'),
		props: true
	},

	{
		path: '/index/register',
		name: 'usersuserregister',
		component: () => import('./pages/index/userregister.vue'),
		props: true
	},

	{
		path: '/index/student-register',
		name: 'studentregister',
		component: () => import('./pages/index/studentregister.vue'),
		props: true
	},
	//教师注册路由
	{
		path: '/index/teacher-register',
		name: 'teacherregister',
		component: () => import('./pages/index/teacherregister.vue'),
		props: true
	},

	{
		path: '/account/edit',
		name: 'usersaccountedit',
		component: () => import('./pages/account/accountedit.vue'),
		props: true
	},

	{
		path: '/account',
		name: 'usersaccountview',
		component: () => import('./pages/account/accountview.vue'),
		props: true
	},

	{
		path: '/users/add',
		name: 'usersadd',
		component: () => import('./pages/users/add.vue'),
		props: true
	},

	{
		path: '/users/edit/:id',
		name: 'usersedit',
		component: () => import('./pages/users/edit.vue'),
		props: true
	},




	//Password reset routes
	{
		path: '/index/forgotpassword',
		name: 'forgotpassword',
		component: () => import('./pages/index/forgotpassword.vue'),
		props: true
	},
	{
		path: '/index/resetpassword',
		name: 'resetpassword',
		component: () => import('./pages/index/resetpassword.vue'),
		props: true
	},
	{
		path: '/index/resetpassword_completed',
		name: 'resetpassword_completed',
		component: () => import('./pages/index/resetpassword_completed.vue'),
		props: true
	},
	{
		path: '/index/employer-apply',
		name: 'employer-apply',
		component: () => import('./pages/index/employerapply.vue'),
		meta: {
			title: '企业API申请'
		}
	},


	{
		path: '/employerapplications',
		name: 'employerapplicationslist',
		component: () => import('./pages/employerapplications/list.vue'),
		meta: {
			title: '企业申请审核',
			requiresAuth: true,
			roles: ['admin', 'eduadmin']
		}
	},



	{
		path: '/error/forbidden',
		name: 'forbidden',
		component: () => import('./pages/errors/forbidden.vue'),
		props: true
	},
	{
		path: '/error/notfound',
		name: 'notfound',
		component: () => import('./pages/errors/pagenotfound.vue'),
		props: true
	},
	{
		path: '/:catchAll(.*)',
		component: () => import('./pages/errors/pagenotfound.vue')
	}
];

export default () => {

	const auth = useAuth();


	const user = auth.user;
	if (user) {
		routes.push({ path: '/', alias: '/home', name: 'home', component: () => import(`./pages/home/home.vue`) });
	}
	else {
		routes.push({ path: '/', alias: '/index', name: 'index', component: () => import('./pages/index/index.vue') });
	}

	const router = createRouter({
		history: createWebHashHistory(),
		routes,
		scrollBehavior(to, from, savedPostion) {
			if (savedPostion) return savedPostion;
			return { top: 0 }
		}
	});

	router.beforeEach((to, from, next) => {
		const user = auth.user;
		let path = to.path;
		let authRequired = auth.pageRequiredAuth(path);
		if (authRequired) {
			if (!user) {
				return next({ path: '/', query: { nexturl: to.fullPath } });
			}
			//authorize user
			if (!auth.canView(path)) {
				return next({ path: "/error/forbidden" });
			}
		}

		if (user && to.name == "index") {
			//already logged in, show home when try to access index page
			return next({ path: "/home" });
		}

		//navigate to redirect url if available
		if (to.name == "home" && to.query.nexturl) {
			return next({ path: to.query.nexturl });
		}

		//close dialog from previous page
		//store.closeDialogs('app/closeDialogs');

		next();
	});

	return router;
}
